#include "GameMain.h"

buildMediator *buildMediator::initCreate()
{
	buildMediator *build = NULL;
	do
	{
		build = buildMediator::node();
		CC_BREAK_IF(!build);
	}while(0);
	GameMain::sharedGameMain()->getHeroLayer()->addChild(build);
	return build;
}

void buildMediator::registerWithTouchDispatcher()
{
	this->onEnter();
}

bool buildMediator::init()
{
	bool flag = false;
	do
	{
		CC_BREAK_IF(! CCSprite::init());
		this->schedule(schedule_selector(buildMediator::update),1,kCCRepeatForever,0.0f);
		
		flag = true;
	}while(0);
	return flag;
}

void buildMediator::onEnter()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-128,true);
}
void buildMediator::onExit()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

buildModel *buildMediator::getModel()
{
	return model;
}

void buildMediator::setModel(buildModel *value)
{
	model = value;
	model->setView(this);
}

void buildMediator::update(float dt)
{
}

void buildMediator::dataChange(bool value)
{
	if(value)
	{
		this->initWithTexture(GameLoader::sharedGameLoader()->getTexture2d("CloseNormal.png"));
		this->setPosition(GameOther::getViewPoint(model->getCurrentX(),model->getCurrentY()));
	}
	else
	{
		
	}
}

void buildMediator::stand()
{
	CCAnimate *animate = GameOther::getAnimate(model->getId(), "stand.png");
	this->runAction(CCSequence::create(animate));
}

void buildMediator::attack()
{
	CCAnimate *animate = GameOther::getAnimate(model->getId(),"attack.png");
	this->runAction(CCSequence::create(animate,callfunc_selector(buildMediator::end),NULL));
}

void buildMediator::skill()
{
	CCAnimate *animate = GameOther::getAnimate(model->getId(),"skill.png");
	this->runAction(CCSequence::create(animate,callfunc_selector(buildMediator::end),NULL));
}

void buildMediator::die()
{
	CCAnimate *animate = GameOther::getAnimate(model->getId(),"die.png");
	this->runAction(CCSequence::create(animate,callfunc_selector(buildMediator::end),NULL));
}

void buildMediator::end()
{
	this->getActionManager()->removeAllActions();
	buildMediator::stand();
}

bool buildMediator::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	CCRect rect = CCRect(this->getPositionX() - this->getContentSize().width * this->getAnchorPoint().x,
		this->getPositionY() - this->getContentSize().height * this->getAnchorPoint().y,
		this->getContentSize().width,this->getContentSize().height);
	CCPoint point = pTouch->getLocation();
	point.x -= GameMain::sharedGameMain()->getBgLayer()->getPositionX();
	point.y -= GameMain::sharedGameMain()->getBgLayer()->getPositionY();
	bool isTouch = rect.containsPoint(point);
	if(isTouch)
		return true;
	return false;
}
void buildMediator::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
	 
}
void buildMediator::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
}
void buildMediator::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
	 
}